package com.xbongbong.sso.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.sso.domain.entity.XbbUserEntity;
import com.xbongbong.sso.model.XbbUserModel;
import com.xbongbong.sso.pojo.vo.GenerateNonceVO;
import com.xbongbong.sso.service.OauthFlowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

/**
 * IDP用户对应唯一码
 * 创建时间 2020/12/28 2:51 PM
 * 修改时间 2020/12/28 2:51 PM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/idp")
public class IdpTestController {

    private static final Logger LOG = LoggerFactory.getLogger(IdpTestController.class);

    @Resource
    private OauthFlowService oauthFlowService;
    @Resource
    private XbbUserModel xbbUserModel;
    /**
     * token获取
     * @param params 关联DTO
     */
    @PostMapping(value = "/tokenPostForm")
    @ResponseBody
    public JSONObject tokenPostForm(@RequestParam Map<String, String> params) throws XbbException{
        String oauthCode = params.get("oauthCode");
        String appId = params.get("appId");

        if (!Objects.equals(oauthCode, "oauthCode_post")) {
            throw new XbbException(1001, "oauthCode错误");
        }

        if (!Objects.equals(appId, "appId_post")) {
            throw new XbbException(1001, "appId错误");
        }
        JSONObject retJson = new JSONObject();
        retJson.put("accessToken", "accessToken_post");
        retJson.put("userId", "userId_post");
        return retJson;
    }

    /**
     * token获取
     * @param json 关联DTO
     */
    @RequestMapping(value = "/tokenPost", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String tokenPost(@RequestBody @Valid JSONObject json) throws XbbException{
        String oauthCode = json.getString("oauthCode");
        String appId = json.getString("appId");

        if (!Objects.equals(oauthCode, "oauthCode_post")) {
            throw new XbbException(1001, "oauthCode错误");
        }

        if (!Objects.equals(appId, "appId_post")) {
            throw new XbbException(1001, "appId错误");
        }
        JSONObject retJson = new JSONObject();
        retJson.put("accessToken", "accessToken_post");
        retJson.put("userId", "userId_post");
        return JSON.toJSONString(retJson);
    }

    /**
     * token获取
     * @param httpServletRequest 关联DTO
     */
    @RequestMapping(value = "/tokenGet", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String tokenGet(HttpServletRequest httpServletRequest) {
        XbbResponse<JSONObject> response = null;
        JSONObject retJson = new JSONObject();
        retJson.put("accessToken", "accessToken_get");
        response = new XbbResponse<>(retJson);
        return JSON.toJSONString(response);
    }

    /**
     * 用户信息获取
     * @param json 关联DTO
     */
    @RequestMapping(value = "/idAuthPost", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String idAuthPost(@RequestBody @Valid JSONObject json) throws Exception{
        JSONObject retJson = new JSONObject();
        retJson.put("userId", "userId_post");
        return JSON.toJSONString(retJson);
    }

    /**
     * 用户信息获取
     * @param httpServletRequest 关联DTO
     */
    @RequestMapping(value = "/idAuthGet", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String idAuthGet(HttpServletRequest httpServletRequest) {
        XbbResponse<JSONObject> response = null;
        JSONObject retJson = new JSONObject();
        retJson.put("userId", "userId_get");
        response = new XbbResponse<>(retJson);
        return JSON.toJSONString(response);
    }

    /**
     * 用户信息获取
     * @param json 关联DTO
     */
    @RequestMapping(value = "/getLoginUrl", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getLoginUrl(@RequestBody @Valid JSONObject json) {
        XbbResponse<JSONObject> response = null;
        JSONObject retJson = new JSONObject();
        XbbUserEntity user = xbbUserModel.getByUserId("1", "1");
        try {
            GenerateNonceVO generateNonceVO = oauthFlowService.generateNonce(user);
            String loginUrl = generateNonceVO.getWebFrontUrl() + "/auto-login.html?t=" + generateNonceVO.getU()
                    + "&nonce=" + generateNonceVO.getNonce();
            retJson.put("loginUrl", loginUrl);
        } catch (XbbException e) {
            throw new RuntimeException(e);
        }
        response = new XbbResponse<>(retJson);
        return JSON.toJSONString(response);
    }


}
